package gmu.swe.project.io;

import java.io.File;
import java.util.ArrayList;

/**
 * Reads the directory provided and adds all files to a PeerInfo object.
 * 
 * @author Christina
 *
 */
public class DirectoryReader {
	
	private String directoryName;
	private ArrayList filesInDirectory;
	
	/**
	 * Creates a new DirectoryReader; expects the File Share directory to be passed in (stored
	 * in configuration file).
	 * @param directory
	 */
	public DirectoryReader(String directoryName) {
		this.directoryName = directoryName;
		filesInDirectory = new ArrayList();
	}
	
	/**
	 * Reads the directory provided and adds the names of the files found to the list.
	 * If successful, even in empty, returns true;
	 * If directory name provided is not a directory, returns false.
	 * @return boolean
	 */
	public boolean queryDirectory() {
		//make sure a directory name was passed in
		if(directoryName == null || directoryName.equals(""))
			return false;
		
		File directory = new File(directoryName);
		if(directory.isDirectory()) {
			File[] files = directory.listFiles();
		    for (int i = 0; i < files.length; i++) {
		        File file = (File)files[i];
		        String filename = file.getName();
		        filesInDirectory.add(filename);
		    }
			return true;
		} else {
			return false;
		}
	}

	/** Accessors **/
	public String getDirectoryName() {
		return directoryName;
	}
	public void setDirectoryName(String directoryName) {
		this.directoryName = directoryName;
	}
	public ArrayList getFilesInDirectory() {
		return filesInDirectory;
	}
	public void setFilesInDirectory(ArrayList filesInDirectory) {
		this.filesInDirectory = filesInDirectory;
	}

}
